package org.aceor.mddal.common.monitor;

import com.google.common.collect.Maps;
import org.aceor.mddal.common.monitor.handler.*;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by lxue on 16/4/5.
 */
public class MonitorPointExecutor {
    private static final Executor cacheExecutor = Executors.newCachedThreadPool();

    private static final Map<MonitorPoint, MonitorPointHandler> handlers = Maps.newHashMap();


    static {
        handlers.put(MonitorPoint.CONNECTION_CONTEXT, new ConnectionContextHandler());

        handlers.put(MonitorPoint.EXECUTE_SQL, new ExecuteSqlHandler());

        handlers.put(MonitorPoint.GET_CONNECTION, new GetConnectionHandler());

        handlers.put(MonitorPoint.PARSE_SQL, new ParseSqlHandler());
    }

    public static void execute(MonitorPoint point, long costTime, String[] params) {
        if (!handlers.containsKey(point)) {
            throw new IllegalArgumentException();
        }
        cacheExecutor.execute(new Runnable() {
            @Override
            public void run() {
                handlers.get(point).print(costTime, params);
            }
        });
    }
}
